﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using System.Data;
using  DataModel ;
using System.Web.Security;
using WebMatrix.WebData;
using ChurchEdu.Models;
namespace ChurchEdu.Controllers
{
    [Authorize]
    public class CategoryController : Controller
    {
        //
        // GET: /Category/
        private Church_DataEntities  db;

        public CategoryController()
        {
            db = new Church_DataEntities();     
        }

        public ActionResult Index()
        {
          //  var categorylist = from a in db.Category select a;
         //   IEnumerable<Categorylist> aa=new List<Categorylist>();
          //  var query=from a in db.Category where 
          


            return View();
        }


        public ActionResult getmenu()
        {
            var list = (from a in db.Directory
                        select new
                        {
                            id = a.ID,
                            pId = a.Parent_ID,
                            name = a.Dire_Name
,                         
                        }).ToList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        //
        // GET: /Category/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Category/Create
         //[Authorize(Roles = "Admin")] 
        public ActionResult Create()
        {
            return PartialView();
        }

         //[Authorize(Roles = "Admin")] 
        public ActionResult CreateSubmenu(int ID, FormCollection collection)
        {

            if (collection["submenuname"].Trim() != null && collection["submenuname"].Trim() !="")
            {
                Directory aa = new Directory();
                aa.Parent_ID = ID;
                aa.Dire_Name = collection["submenuname"];
                db.Directory.Add(aa);
                db.SaveChanges();
     
            }
            return RedirectToAction("Index"); 

           // return PartialView();
        }
        //
        // POST: /Category/Create
         //[Authorize(Roles = "Admin")] 
        [HttpPost]
        public ActionResult Create( CreateCategory  model)
        {
            if (ModelState.IsValid)
            {
                var categoryname = (from a in db.Directory where a.Dire_Name == model.FirstLevelCategoryName select a).Count();
                if (categoryname > 0)

                { 
                    ModelState.AddModelError("", "类别名称已经存在！");              
                    return PartialView();
                
                }
                Directory aa = new Directory();
                aa.Parent_ID = 0;
                aa.Dire_Name = model.FirstLevelCategoryName;
                db.Directory.Add(aa);
                db.SaveChanges();
                return RedirectToAction("Index");      

            }

            return PartialView();

        }

        //
        // GET: /Category/Edit/5
        //[Authorize(Roles = "Admin")] 
        public ActionResult Edit(int ID=1)
        {
            ViewData["ID"] = ID;
            if (Request.IsAjaxRequest())
            {
                var aa = db.Directory.Single(a => a.ID==ID);
                EditCategory bb = new EditCategory();
                bb.ID = aa.ID;
                bb.parentid = aa.Parent_ID;
                bb.CategoryName = aa.Dire_Name;
                return PartialView(bb);
            }   
            else
                return null;
        }

        //
        // POST: /Category/Edit/5
         //[Authorize(Roles = "Admin")] 
        [HttpPost]
        //[ActionName("Edit")]
        public ActionResult EditComfirm( EditCategory model)
        {
            if (ModelState.IsValid)
            {
                Directory aa = db.Directory.Single(a => a.ID == model.ID);
                aa.Dire_Name = model.CategoryName;
                db.Directory.Attach(aa);
                db.Entry(aa).State = EntityState.Modified;
                db.SaveChanges();
               
            }
            return RedirectToAction("Index");
        }

        //
        // GET: /Category/Delete/5
         //[Authorize(Roles = "Admin")] 
        public ActionResult DeleteCategory(int ID)
        {
            //if ((from a in db.ResourceCategory where a.CategoryID == ID select a).Count() > 0)
            //{
            //    var script = String.Format("<script>alert('zip资源包解析错误，请重新上传zip包');location.href='{0}'</script>", Url.Action("Index", "Category"));
            //    return Content(script, "text/html");
            //}
            var bb = from a in db.Directory where a.Parent_ID == ID select a;
                foreach (var ss in bb) 
                {
                    db.Directory.Remove(ss);                           
                }
                Directory aa = (from a in db.Directory where a.ID == ID select a).Single();
                db.Directory.Remove(aa);
               db.SaveChanges();

            return RedirectToAction("Index");
        }

        //
        // POST: /Category/Delete/5
        //[Authorize(Roles = "Admin")] 
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }




        public ActionResult GetAllCategory() 
        {





            return View();
        }





    }
}
